package com.ph.irp.base.verifyproject.service.impl;
import java.util.ArrayList;
import java.util.List;
import com.ph.irp.base.verifyproject.dao.PBaseVerifyprojectCandiateDao;
import com.ph.irp.base.verifyproject.dao.PBaseVerifyprojectStorageDao;
import com.ph.irp.base.verifyproject.dao.PBaseVerifyprojectStoragetypeDao;
import com.ph.irp.base.verifyproject.service.PBaseVerifyprojectStorageService;
import com.ph.irp.dto.StorageDTO;
import com.ph.irp.model.PBaseVerifyprojectCandiate;
import com.ph.irp.model.PBaseVerifyprojectStorage;
import com.ph.irp.model.PBaseVerifyprojectStoragetype;

public class PBaseVerifyprojectStorageServiceImpl implements
		PBaseVerifyprojectStorageService {
	private PBaseVerifyprojectStorageDao storageDaoProxy;
	private PBaseVerifyprojectStoragetypeDao storageTypeDaoProxy;
	private PBaseVerifyprojectCandiateDao candiateDaoProxy;

	public PBaseVerifyprojectStorageDao getStorageDaoProxy() {
		return storageDaoProxy;
	}

	public void setStorageDaoProxy(PBaseVerifyprojectStorageDao storageDaoProxy) {
		this.storageDaoProxy = storageDaoProxy;
	}

	public PBaseVerifyprojectStoragetypeDao getStorageTypeDaoProxy() {
		return storageTypeDaoProxy;
	}

	public void setStorageTypeDaoProxy(
			PBaseVerifyprojectStoragetypeDao storageTypeDaoProxy) {
		this.storageTypeDaoProxy = storageTypeDaoProxy;
	}

	public PBaseVerifyprojectCandiateDao getCandiateDaoProxy() {
		return candiateDaoProxy;
	}

	public void setCandiateDaoProxy(
			PBaseVerifyprojectCandiateDao candiateDaoProxy) {
		this.candiateDaoProxy = candiateDaoProxy;
	}

	@Override
	public void addStorage(PBaseVerifyprojectStorage storage) throws Throwable {
		if (storageDaoProxy.isSame(storage.getStorageCode())) {
			throw new Throwable("代码【" + storage.getStorageCode() + "】已经存在");
		} else {
			storageDaoProxy.addStorage(storage);
		}
	}

	@Override
	public void deleteStorage(int storageId) throws Throwable {
		storageDaoProxy.deleteStorage(storageId);

	}

	@Override
	public StorageDTO getStorageById(int storageId) throws Throwable {
		StorageDTO s = new StorageDTO();
		PBaseVerifyprojectStorage storage = storageDaoProxy
				.getStorageById(storageId);
		int PBaseVerifyprojectCandiateId = storage
				.getPBaseVerifyprojectCandiateId();
		int PBaseVerifyprojectStoragetypeId = storage
				.getPBaseVerifyprojectStoragetypeId();
		s.setStorage(storage);
		if (PBaseVerifyprojectCandiateId != 0) {
			PBaseVerifyprojectCandiate candiate = candiateDaoProxy
					.getCandiateById(PBaseVerifyprojectCandiateId);
			s.setCandiate(candiate);
		}
		if (PBaseVerifyprojectStoragetypeId != 0) {
			PBaseVerifyprojectStoragetype storageType = storageTypeDaoProxy
					.getStorageTypeById(PBaseVerifyprojectStoragetypeId);
			s.setStorageType(storageType);
		}
		return s;
	}

	@Override
	public List<StorageDTO> getStorageList(PBaseVerifyprojectStorage storage)
			throws Throwable {
		List<StorageDTO> storages = new ArrayList<StorageDTO>();
		List<PBaseVerifyprojectStorage> list = storageDaoProxy
				.getStorageList(storage);
		for (PBaseVerifyprojectStorage pbvs : list) {
			StorageDTO s = new StorageDTO();

			int PBaseVerifyprojectCandiateId = pbvs
					.getPBaseVerifyprojectCandiateId();
			int PBaseVerifyprojectStoragetypeId = pbvs
					.getPBaseVerifyprojectStoragetypeId();
			s.setStorage(pbvs);
			if (PBaseVerifyprojectCandiateId != 0) {
				PBaseVerifyprojectCandiate candiate = candiateDaoProxy
						.getCandiateById(PBaseVerifyprojectCandiateId);
				s.setCandiate(candiate);
			}
			if (PBaseVerifyprojectStoragetypeId != 0) {
				PBaseVerifyprojectStoragetype storageType = storageTypeDaoProxy
						.getStorageTypeById(PBaseVerifyprojectStoragetypeId);
				s.setStorageType(storageType);
			}
			storages.add(s);
		}

		return storages;
	}

	@Override
	public void updateStorage(PBaseVerifyprojectStorage storage)
			throws Throwable {
		PBaseVerifyprojectStorage pbvs = storageDaoProxy.getStorageById(storage
				.getStorageId());
		if (storage.getStorageCode().equals(pbvs.getStorageCode())) {
			storageDaoProxy.updateStorage(storage);
		} else {
			if (storageDaoProxy.isSame(storage.getStorageCode())) {
				throw new Throwable("代码【" + storage.getStorageCode() + "】已经存在");
			} else {
				storageDaoProxy.updateStorage(storage);
			}
		}
	}

}
